//
//  OrderTableViewCell.m
//  UEAssistant
//
//  Created by mac on 2017/1/22.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "OrderTableViewCell.h"
#import "FillLabelView.h"

@interface OrderTableViewCell (){
    
    BOOL     _ifBindTags;
}

@property (nonatomic, strong) UIView                                              *backView;
@property (nonatomic, strong) UIView                                              *noticView;
@property (nonatomic, strong) UILabel                                             *nameLabel;
@property (nonatomic, strong) UILabel                                             *wordCount;
@property (nonatomic, strong) UILabel                                             *langugeType;
@property (nonatomic, strong) UILabel                                             *timeLabel;
@property (nonatomic, strong) UIImageView                                         *headView;
@property (nonatomic, strong) UIImageView                                         *yuImage;
@property (nonatomic, strong) UIImageView                                         *timeImage;
@property (nonatomic, strong) UILabel                                             *orderType;
@property (nonatomic, strong) UIImageView                                         *historyGrayView;
@property (nonatomic, strong) UIImageView                                         *typeImageVV;
@property (nonatomic, strong) FillLabelView                                       *fillLabelView;

@end

@implementation OrderTableViewCell

+(instancetype)cellWithTableView:(UITableView *)tableView{
    
    static NSString *identifier = @"orderTableCell";
    OrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[OrderTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
        cell.backgroundColor = [UIColor whiteColor];
        UIColor *color = [UIColor colorWithWhite:0.9 alpha:0.3];
        cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
        cell.selectedBackgroundView.backgroundColor = color;
    }
    else{
        if (cell.fillLabelView.subviews.count > 0) {
            [cell.fillLabelView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                [obj removeFromSuperview];
            }];
        }
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.backgroundColor = [UIColor whiteColor];
    
    _noticView = [[UIView alloc]initWithFrame:CGRectMake(20*Radio, 20*Radio+2*Radio, 10*Radio, 10*Radio)];
    _noticView.backgroundColor = RGB(30, 172, 242, 1);
    _noticView.layer.cornerRadius = 5*Radio;
    _noticView.layer.masksToBounds = YES;
    _noticView.layer.shouldRasterize = YES; //圆角缓存
    _noticView.layer.rasterizationScale = [UIScreen mainScreen].scale;//提高流畅度
    [self addSubview:_noticView];
    
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(_noticView.frame.origin.x+_noticView.frame.size.width+8*Radio, _noticView.frame.origin.y-10*Radio, 230*Radio, 30*Radio)];
    _nameLabel.backgroundColor = [UIColor whiteColor];
    _nameLabel.textColor = RGB(51, 51, 51, 1);
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    _nameLabel.font = [UIFont boldSystemFontOfSize:FLEXIBLE(15)];
    [self addSubview:_nameLabel];
    
    _typeImageVV = [[UIImageView alloc]initWithFrame:CGRectMake(Screen_Width - 60, 0, 38*Radio, 38*Radio)];
    _typeImageVV.backgroundColor = [UIColor whiteColor];
    _typeImageVV.image = [UIImage imageNamed:@"翻译"];
    [self addSubview:_typeImageVV];
    
    _timeImage = [[UIImageView alloc]initWithFrame:CGRectMake(_noticView.frame.origin.x-2*Radio, _nameLabel.frame.origin.y+_nameLabel.frame.size.height+5*Radio, 16*Radio, 16*Radio)];
    _timeImage.backgroundColor = [UIColor whiteColor];
    _timeImage.image = [UIImage imageNamed:@"时间"];
    [self addSubview:_timeImage];
    
    _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(_timeImage.frame.origin.x+_timeImage.frame.size.width+10*Radio, _timeImage.frame.origin.y+_timeImage.frame.size.height/2-10*Radio, 200*Radio, 20*Radio)];
    _timeLabel.backgroundColor = [UIColor whiteColor];
    _timeLabel.textColor = RGB(87, 87, 87, 1);
    _timeLabel.textAlignment = NSTextAlignmentLeft;
    _timeLabel.font = [UIFont systemFontOfSize:FLEXIBLE(14)];
    [self addSubview:_timeLabel];
    
    _yuImage = [[UIImageView alloc]initWithFrame:CGRectMake(_timeImage.frame.origin.x, _timeImage.frame.origin.y+_timeImage.frame.size.height+13*Radio, 16*Radio, 16*Radio)];
    _yuImage.backgroundColor = [UIColor whiteColor];
    _yuImage.image = [UIImage imageNamed:@"语言"];
    [self addSubview:_yuImage];
    
    _langugeType = [[UILabel alloc]initWithFrame:CGRectMake(_yuImage.frame.origin.x+_yuImage.frame.size.width+10, _yuImage.frame.origin.y+_yuImage.frame.size.height/2-10*Radio, 220*Radio, 20*Radio)];
    _langugeType.backgroundColor = [UIColor whiteColor];
    _langugeType.textColor = RGB(87, 87, 87, 1);
    _langugeType.textAlignment = NSTextAlignmentLeft;
    _langugeType.font = [UIFont systemFontOfSize:FLEXIBLE(14)];
    [self addSubview:_langugeType];
    
    _wordCount = [[UILabel alloc]initWithFrame:CGRectMake(Screen_Width - 15*Radio - 90*Radio, _yuImage.frame.origin.y+_yuImage.frame.size.height+6*Radio, 90*Radio, 30*Radio)];
    _wordCount.backgroundColor = [UIColor whiteColor];
    _wordCount.textColor = RGB(0, 145, 251, 1);
    _wordCount.textAlignment = NSTextAlignmentRight;
    _wordCount.font = [UIFont systemFontOfSize:FLEXIBLE(14) weight:0.01];
    [self addSubview:_wordCount];
    
    _historyGrayView = [[UIImageView alloc]initWithFrame:CGRectMake(Screen_Width - 50*Radio, _yuImage.frame.origin.y+13*Radio, 50*Radio, 50*Radio)];
    _historyGrayView.backgroundColor = [UIColor whiteColor];
    _historyGrayView.image = [UIImage imageNamed:@"已抢-编辑"];
    [self addSubview:_historyGrayView];
    
    
}

#pragma mark -- public
- (void)removeTypeView{
    
    [_orderType removeFromSuperview];
}

- (void)showHisGray{
    
    [_historyGrayView setHidden:NO];
}

- (void)hiddenHisGray{
    
    [_historyGrayView setHidden:YES];
}

#pragma mark -- setData
-(void)dispatchData:(id)model cellModelType:(CellModelType)modelType{
    
    switch (modelType) {
        case ModelTypeTeamWait:
            {
                if (![model isKindOfClass:[TeamWaitModel class]]) return;
                TeamWaitModel *teamWait = model;
                [_wordCount setHidden: NO];
                [self hiddenHisGray];
                
                //历史订单（为了避免全是历史订单，不等式会存在负数，所以两边均+1,避免出现此种情况）
                if (teamWait.indexPath_row + 1 > teamWait.OrderTotelCount - orderSingleton.historyCount - 1 + 1) {
                    [_noticView removeFromSuperview];
                    [_nameLabel setFrame:CGRectMake(20*Radio, 12*Radio, 230*Radio, 30*Radio)];
                    [self showHisGray];
                    [_wordCount setHidden:YES];
                    
                    //替换小旗子
                    if ([teamWait.taskType isEqualToString:@"翻译"]) {
                        _typeImageVV.image = [UIImage imageNamed:@"翻译_pre"];
                    }else if ([teamWait.taskType isEqualToString:@"编辑"]){
                        _typeImageVV.image = [UIImage imageNamed:@"编辑_pre"];
                    }else{
                        _typeImageVV.image = [UIImage imageNamed:@"校对_pre"];
                    }
                }
                else{//新订单
                    if ([teamWait.isRead integerValue] == 1) {
                        [_noticView removeFromSuperview];
                        [_nameLabel setFrame:CGRectMake(20*Radio, 12*Radio, 230*Radio, 30*Radio)];
                    }
                    else{
                        [self addSubview:_noticView];
                        [_nameLabel setFrame:CGRectMake(_noticView.frame.origin.x+_noticView.frame.size.width+8*Radio,12*Radio, 230*Radio, 30*Radio)];
                    }
                    
                    if ([teamWait.taskType isEqualToString:@"翻译"]) {
                        _typeImageVV.image = [UIImage imageNamed:@"翻译"];
                    }else if ([teamWait.taskType isEqualToString:@"编辑"]){
                        _typeImageVV.image = [UIImage imageNamed:@"编辑"];
                    }else{
                        _typeImageVV.image = [UIImage imageNamed:@"校对"];
                    }
                }
                
                _wordCount.text = [NSString stringWithFormat:@"%@字",teamWait.wordCount];
                _nameLabel.text = teamWait.taskName;
                _langugeType.text = [NSString stringWithFormat:@"%@ -> %@",teamWait.sourceLangValue,teamWait.targetLangValue];
                _timeLabel.text = [NSString stringWithFormat:@"%@",teamWait.deadLineTime];
                
                //添加自定义标签
                NSArray *types = [teamWait.labels componentsSeparatedByString:@"、"];
                [self addFillLabelViewWithTags:types calculateHightWithModel:teamWait modelType:ModelTypeTeamWait];

                //当没有标签时 改变部分子视图的高度
                if ([teamWait.labels isEqualToString:@""]) {
                    [_historyGrayView setFrame:CGRectMake(Screen_Width - 50*Radio, _yuImage.frame.origin.y-20*Radio, 50*Radio, 50*Radio)];
                    [_wordCount setFrame:CGRectMake(Screen_Width - 10*Radio - 90*Radio, _yuImage.frame.origin.y, 90*Radio, 30*Radio)];
                }
                else{
                    [_historyGrayView setFrame:CGRectMake(Screen_Width - 50*Radio, _yuImage.frame.origin.y+13*Radio, 50*Radio, 50*Radio)];
                    [_wordCount setFrame:CGRectMake(Screen_Width - 15*Radio - 90*Radio, _yuImage.frame.origin.y+_yuImage.frame.size.height+6*Radio, 90*Radio, 30*Radio)];
                }
                if (_ifBindTags) {
                    [_historyGrayView setFrame:CGRectMake(Screen_Width - 50*Radio, _yuImage.frame.origin.y+33*Radio, 50*Radio, 50*Radio)];
                }
            }
            break;
        case ModelTypeTeamIng:
            {
                if (![model isKindOfClass:[TeamIngModel class]]) return;
                TeamIngModel *teamIng = model;
                [_wordCount setHidden: NO];
                [self hiddenHisGray];
                _wordCount.text      = [NSString stringWithFormat:@"%@字",teamIng.wordCount];
                
                if ([teamIng.taskType isEqualToString:@"翻译"]) {
                    _typeImageVV.image = [UIImage imageNamed:@"翻译"];
                }else if ([teamIng.taskType isEqualToString:@"编辑"]){
                    _typeImageVV.image = [UIImage imageNamed:@"编辑"];
                }else{
                    _typeImageVV.image = [UIImage imageNamed:@"校对"];
                }
                
                [_noticView removeFromSuperview];
                [_nameLabel setFrame:CGRectMake(20*Radio, 12*Radio, 230*Radio, 30*Radio)];
                _nameLabel.text = teamIng.taskName;
                _timeLabel.text = teamIng.deadLineTime;
                _langugeType.text = [NSString stringWithFormat:@"%@ -> %@",teamIng.sourceLangValue,teamIng.targetLangValue];;
                
                NSArray *types = [self interceptTagsWithListString:teamIng.labels];
                [self addFillLabelViewWithTags:types calculateHightWithModel:teamIng modelType:ModelTypeTeamIng];

                if ([teamIng.labels isEqualToString:@""]) {
                    [_wordCount setFrame:CGRectMake(Screen_Width - 10*Radio - 90*Radio, _yuImage.frame.origin.y-6*Radio, 90*Radio, 30*Radio)];
                }
                else{
                    [_wordCount setFrame:CGRectMake(Screen_Width - 15*Radio - 90*Radio, _yuImage.frame.origin.y+_yuImage.frame.size.height+6*Radio, 90*Radio, 30*Radio)];
                }
            }
            break;
        case ModelTypePlatWait:
            {
                if (![model isKindOfClass:[PlatWaitModel class]]) return;
                PlatWaitModel *platWait = model;
                [_wordCount setHidden: NO];
                _wordCount.text = [NSString stringWithFormat:@"%@字",platWait.wordcount];
                _nameLabel.text = platWait.doc_name;
                _timeLabel.text = platWait.deadline_time;
                _langugeType.text = [NSString stringWithFormat:@"%@ -> %@",platWait.sourcelangname,platWait.targetlangname];
                
                if ([platWait.isRead integerValue] == 1) {
                    [_noticView removeFromSuperview];
                    [_nameLabel setFrame:CGRectMake(20*Radio, 12*Radio, 230*Radio, 30*Radio)];
                }
                else{
                    [self addSubview:_noticView];
                    [_nameLabel setFrame:CGRectMake(_noticView.frame.origin.x+_noticView.frame.size.width+8*Radio, 12*Radio, 230*Radio, 30*Radio)];
                }
                [self hiddenHisGray];
                
                if ([platWait.tasktype integerValue] == 1 ) {
                    _typeImageVV.image = [UIImage imageNamed:@"翻译"];
                }else if ([platWait.tasktype integerValue] == 2){
                    _typeImageVV.image = [UIImage imageNamed:@"编辑"];
                }else{
                    _typeImageVV.image = [UIImage imageNamed:@"校对"];
                }
                
                NSArray *types = [self interceptTagsWithListString:platWait.label_idname_list];
                [self addFillLabelViewWithTags:types calculateHightWithModel:platWait modelType:ModelTypePlatWait];

                if ([platWait.label_idname_list isEqualToString:@""]) {
                    [_historyGrayView setFrame:CGRectMake(Screen_Width - 50*Radio, _yuImage.frame.origin.y-20*Radio, 50*Radio, 50*Radio)];
                    [_wordCount setFrame:CGRectMake(Screen_Width - 10*Radio - 90*Radio, _yuImage.frame.origin.y, 90*Radio, 30*Radio)];
                }
                else{
                    [_historyGrayView setFrame:CGRectMake(Screen_Width - 50*Radio, _yuImage.frame.origin.y+13*Radio, 50*Radio, 50*Radio)];
                    [_wordCount setFrame:CGRectMake(Screen_Width - 15*Radio - 90*Radio, _yuImage.frame.origin.y+_yuImage.frame.size.height+6*Radio, 90*Radio, 30*Radio)];
                }
                if (_ifBindTags) {
                    [_historyGrayView setFrame:CGRectMake(Screen_Width - 50*Radio, _yuImage.frame.origin.y+33*Radio, 50*Radio, 50*Radio)];
                }
                
            }
            break;
        case ModelTypePlatIng:
            {
                if (![model isKindOfClass:[PlatIngModel class]]) return;
                PlatIngModel *platIng = model;
                [_wordCount setHidden: NO];
                [self hiddenHisGray];
                _wordCount.text      = [NSString stringWithFormat:@"%@字",platIng.wordcount];
                
                if ([platIng.tasktype integerValue] == 1 ) {
                    _typeImageVV.image = [UIImage imageNamed:@"翻译"];
                }else if ([platIng.tasktype integerValue] == 2){
                    _typeImageVV.image = [UIImage imageNamed:@"编辑"];
                }else{
                    _typeImageVV.image = [UIImage imageNamed:@"校对"];
                }
                
                [_noticView removeFromSuperview];
                [_nameLabel setFrame:CGRectMake(20*Radio, 12*Radio, 230*Radio, 30*Radio)];
                _nameLabel.text = platIng.doc_name;
                _timeLabel.text = platIng.deadline_time;
                _langugeType.text = [NSString stringWithFormat:@"%@ -> %@",platIng.sourcelangname,platIng.targetlangname];
                
                NSArray *types = [self interceptTagsWithListString:platIng.label_idname_list];
                [self addFillLabelViewWithTags:types calculateHightWithModel:platIng modelType:ModelTypePlatIng];
                
                if ([platIng.label_idname_list isEqualToString:@""]) {
                    [_wordCount setFrame:CGRectMake(Screen_Width - 10*Radio - 90*Radio, _yuImage.frame.origin.y-10*Radio, 90*Radio, 30*Radio)];
                }
                else{
                    [_wordCount setFrame:CGRectMake(Screen_Width - 15*Radio - 90*Radio, _yuImage.frame.origin.y+_yuImage.frame.size.height+6*Radio, 90*Radio, 30*Radio)];
                }
            }
            break;
        default:
            break;
    }
}


#pragma mark -- private
- (NSArray *)interceptTagsWithListString:(NSString *)string{
    
    NSMutableArray *types = [NSMutableArray array];
    if (string.length != 0) {
        NSArray *tempArr = [string componentsSeparatedByString:@","];
        [tempArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            @try{
                [types addObject:[obj substringFromIndex:NSMaxRange([obj rangeOfString:@"_"])]];
            }
            @catch (NSException *exception){
                TGLog(@"---private sel assert debug---");
            }
            @finally{

            }
        }];
    }
    return types;
}

- (void)addFillLabelViewWithTags:(NSArray<NSString *> *)tags calculateHightWithModel:(_Nullable id)model modelType:(CellModelType)modelType{

    if (tags.count > 0 && ![tags[0] isEqualToString:@""]) {
        
        //添加标签
        _ifBindTags = [self.fillLabelView bindTags:tags];
        [self addSubview:self.fillLabelView];
        
        //设置cell高度
        switch (modelType) {
            case ModelTypeTeamWait:
                ((TeamWaitModel *)model).cellHeight = _ifBindTags ? 160*Radio : 140*Radio;
                break;
            case ModelTypeTeamIng:
                ((TeamIngModel *)model).cellHeight = _ifBindTags ? 160*Radio : 140*Radio;
                break;
            case ModelTypePlatWait:
                ((PlatWaitModel *)model).cellHeight = _ifBindTags ? 160*Radio : 140*Radio;
                break;
            case ModelTypePlatIng:
                ((PlatIngModel *)model).cellHeight = _ifBindTags ? 160*Radio : 140*Radio;
                break;
            default:
                break;
        }
    }
    else{
        switch (modelType) {
            case ModelTypeTeamWait:
                ((TeamWaitModel *)model).cellHeight = 110*Radio;
                break;
            case ModelTypeTeamIng:
                ((TeamIngModel *)model).cellHeight = 110*Radio;
                break;
            case ModelTypePlatWait:
                ((PlatWaitModel *)model).cellHeight = 110*Radio;
                break;
            case ModelTypePlatIng:
                ((PlatIngModel *)model).cellHeight = 110*Radio;;
                break;
            default:
                break;
        }
    }
}


#pragma mark -- getter
- (FillLabelView *)fillLabelView{
    
    if (!_fillLabelView) {
        _fillLabelView = [[FillLabelView alloc] initWithFrame:CGRectMake(20*Radio, 106*Radio, Screen_Width-40*Radio-80*Radio, 60)];
    }
    return _fillLabelView;
}


- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end

